home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / 3d / OglBench.lha / OglBench / Original / oglbench.c < prev   
C/C++ Source or Header  |  1999-01-02  |  33KB  |  1,278 lines

  1. /* oglbench.c -- An OpenGL benchmarking program.
  2.  * Code is derived from dinoshade.c by Mark J. Kilgard,
  3.  * and isosurf.c by Brian Paul.  Combined by Patrick H. Madden,
  4.  * phm@ikm.com
  5.  *
  6.  * This program reads in the file isosurf.dat to obtain a list
  7.  * of triangles for a rendered surface.  To run as a benchmark,
  8.  * press a lowercase b; output is sent to standard out, so you'll
  9.  * probably want to redirect it to a file.
  10.  *
  11.  * Comment block below is from Mark J. Kilgard's dinoshade....
  12.  */
  13.  
  14. /* Copyright (c) Mark J. Kilgard, 1994, 1997.  */
  15.  
  16. /* This program is freely distributable without licensing fees 
  17.    and is provided without guarantee or warrantee expressed or 
  18.    implied. This program is -not- in the public domain. */
  19.  
  20. /* Example for PC game developers to show how to *combine* texturing,
  21.    reflections, and projected shadows all in real-time with OpenGL.
  22.    Robust reflections use stenciling.  Robust projected shadows
  23.    use both stenciling and polygon offset.  PC game programmers
  24.    should realize that neither stenciling nor polygon offset are 
  25.    supported by Direct3D, so these real-time rendering algorithms
  26.    are only really viable with OpenGL. 
  27.    
  28.    The program has modes for disabling the stenciling and polygon
  29.    offset uses.  It is worth running this example with these features
  30.    toggled off so you can see the sort of artifacts that result.
  31.    
  32.    Notice that the floor texturing, reflections, and shadowing
  33.    all co-exist properly. */
  34.  
  35. /* When you run this program:  Left mouse button controls the
  36.    view.  Middle mouse button controls light position (left &
  37.    right rotates light around dino; up & down moves light
  38.    position up and down).  Right mouse button pops up menu. */
  39.  
  40. /* Check out the comments in the "redraw" routine to see how the
  41.    reflection blending and surface stenciling is done.  You can
  42.    also see in "redraw" how the projected shadows are rendered,
  43.    including the use of stenciling and polygon offset. */
  44.  
  45. /* This program is derived from glutdino.c */
  46.  
  47. /* Compile: cc -o oglbench oglbench.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <math.h>       /* for cos(), sin(), and sqrt() */
  53. #include <GL/glut.h>    /* OpenGL Utility Toolkit header */
  54.  
  55. /* Some <math.h> files do not define M_PI... */
  56. #ifndef M_PI
  57. #define M_PI 3.14159265
  58. #endif
  59.  
  60. /* Variable controlling various rendering modes. */
  61. static int stencilReflection = 1, stencilShadow = 1, offsetShadow = 1;
  62. static int renderShadow = 1, renderDinosaur = 1, renderReflection = 1;
  63. static int linearFiltering = 0, useMipmaps = 0, useTexture = 1;
  64. static int reportSpeed = 0;
  65. static int animation = 0;
  66. static GLboolean lightSwitch = GL_TRUE;
  67. static int directionalLight = 1;
  68. static int forceExtension = 0;
  69. static int bench_iter = 10;
  70. static int object_type = 0;
  71. static int shade_type = 0;
  72.  
  73. /* Time varying or user-controled variables. */
  74. static float jump = 0.0;
  75. static float lightAngle = 50.0, lightHeight = 20;
  76. GLfloat angle = -180;   /* in degrees */
  77. GLfloat angle2 = 30;   /* in degrees */
  78.  
  79. int moving, startx, starty;
  80. int lightMoving = 0, lightStartX, lightStartY;
  81.  
  82. enum {
  83.   MISSING, EXTENSION, ONE_DOT_ONE
  84. };
  85. int polygonOffsetVersion;
  86.  
  87. static GLdouble bodyWidth = 3.0;
  88. /* *INDENT-OFF* */
  89. static GLfloat body[][2] = { {0, 3}, {1, 1}, {5, 1}, {8, 4}, {10, 4}, {11, 5},
  90.   {11, 11.5}, {13, 12}, {13, 13}, {10, 13.5}, {13, 14}, {13, 15}, {11, 16},
  91.   {8, 16}, {7, 15}, {7, 13}, {8, 12}, {7, 11}, {6, 6}, {4, 3}, {3, 2},
  92.   {1, 2} };
  93. static GLfloat arm[][2] = { {8, 10}, {9, 9}, {10, 9}, {13, 8}, {14, 9}, {16, 9},
  94.   {15, 9.5}, {16, 10}, {15, 10}, {15.5, 11}, {14.5, 10}, {14, 11}, {14, 10},
  95.   {13, 9}, {11, 11}, {9, 11} };
  96. static GLfloat leg[][2] = { {8, 6}, {8, 4}, {9, 3}, {9, 2}, {8, 1}, {8, 0.5}, {9, 0},
  97.   {12, 0}, {10, 1}, {10, 2}, {12, 4}, {11, 6}, {10, 7}, {9, 7} };
  98. static GLfloat eye[][2] = { {8.75, 15}, {9, 14.7}, {9.6, 14.7}, {10.1, 15},
  99.   {9.6, 15.25}, {9, 15.25} };
  100. static GLfloat lightPosition[4];
  101. static GLfloat lightColor[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */
  102. static GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}, eyeColor[] = {1.0, 0.2, 0.2, 1.0};
  103. /* *INDENT-ON* */
  104.  
  105. /* Nice floor texture tiling pattern. */
  106. static char *circles[] = {
  107.   "....xxxx........",
  108.   "..xxxxxxxx......",
  109.   ".xxxxxxxxxx.....",
  110.   ".xxx....xxx.....",
  111.   "xxx......xxx....",
  112.   "xxx......xxx....",
  113.   "xxx......xxx....",
  114.   "xxx......xxx....",
  115.   ".xxx....xxx.....",
  116.   ".xxxxxxxxxx.....",
  117.   "..xxxxxxxx......",
  118.   "....xxxx........",
  119.   "................",
  120.   "................",
  121.   "................",
  122.   "................",
  123. };
  124.  
  125.  
  126. static void
  127. makeFloorTexture(void)
  128. {
  129.   GLubyte floorTexture[16][16][3];
  130.   GLubyte *loc;
  131.   int s, t;
  132.  
  133.   /* Setup RGB image for the texture. */
  134.   loc = (GLubyte*) floorTexture;
  135.   for (t = 0; t < 16; t++) {
  136.     for (s = 0; s < 16; s++) {
  137.       if (circles[t][s] == 'x') {
  138.     /* Nice green. */
  139.         loc[0] = 0x1f;
  140.         loc[1] = 0x8f;
  141.         loc[2] = 0x1f;
  142.       } else {
  143.     /* Light gray. */
  144.         loc[0] = 0xaa;
  145.         loc[1] = 0xaa;
  146.         loc[2] = 0xaa;
  147.       }
  148.       loc += 3;
  149.     }
  150.   }
  151.  
  152.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  153.  
  154.   if (useMipmaps) {
  155.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  156.       GL_LINEAR_MIPMAP_LINEAR);
  157.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 16, 16,
  158.       GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
  159.   } else {
  160.     if (linearFiltering) {
  161.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  162.     } else {
  163.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  164.     }
  165.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16, 0,
  166.       GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
  167.   }
  168. }
  169.  
  170. enum {
  171.   X, Y, Z, W
  172. };
  173. enum {
  174.   A, B, C, D
  175. };
  176.  
  177. /* Create a matrix that will project the desired shadow. */
  178. void
  179. shadowMatrix(GLfloat shadowMat[4][4],
  180.   GLfloat groundplane[4],
  181.   GLfloat lightpos[4])
  182. {
  183.   GLfloat dot;
  184.  
  185.   /* Find dot product between light position vector and ground plane normal. */
  186.   dot = groundplane[X] * lightpos[X] +
  187.     groundplane[Y] * lightpos[Y] +
  188.     groundplane[Z] * lightpos[Z] +
  189.     groundplane[W] * lightpos[W];
  190.  
  191.   shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
  192.   shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
  193.   shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
  194.   shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
  195.  
  196.   shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
  197.   shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
  198.   shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
  199.   shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
  200.  
  201.   shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
  202.   shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
  203.   shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
  204.   shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
  205.  
  206.   shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
  207.   shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
  208.   shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
  209.   shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
  210.  
  211. }
  212.  
  213. /* Find the plane equation given 3 points. */
  214. void
  215. findPlane(GLfloat plane[4],
  216.   GLfloat v0[3], GLfloat v1[3], GLfloat v2[3])
  217. {
  218.   GLfloat vec0[3], vec1[3];
  219.  
  220.   /* Need 2 vectors to find cross product. */
  221.   vec0[X] = v1[X] - v0[X];
  222.   vec0[Y] = v1[Y] - v0[Y];
  223.   vec0[Z] = v1[Z] - v0[Z];
  224.  
  225.   vec1[X] = v2[X] - v0[X];
  226.   vec1[Y] = v2[Y] - v0[Y];
  227.   vec1[Z] = v2[Z] - v0[Z];
  228.  
  229.   /* find cross product to get A, B, and C of plane equation */
  230.   plane[A] = vec0[Y] * vec1[Z] - vec0[Z] * vec1[Y];
  231.   plane[B] = -(vec0[X] * vec1[Z] - vec0[Z] * vec1[X]);
  232.   plane[C] = vec0[X] * vec1[Y] - vec0[Y] * vec1[X];
  233.  
  234.   plane[D] = -(plane[A] * v0[X] + plane[B] * v0[Y] + plane[C] * v0[Z]);
  235. }
  236.  
  237. void
  238. extrudeSolidFromPolygon(GLfloat data[][2], unsigned int dataSize,
  239.   GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
  240. {
  241.   static GLUtriangulatorObj *tobj = NULL;
  242.   GLdouble vertex[3], dx, dy, len;
  243.   int i;
  244.   int count = dataSize / (2 * sizeof(GLfloat));
  245.  
  246.   if (tobj == NULL) {
  247.     tobj = gluNewTess();  /* create and initialize a GLU
  248.                              polygon * * tesselation object */
  249.     gluTessCallback(tobj, GLU_BEGIN, glBegin);
  250.     gluTessCallback(tobj, GLU_VERTEX, glVertex2fv);  /* semi-tricky */
  251.     gluTessCallback(tobj, GLU_END, glEnd);
  252.   }
  253.   glNewList(side, GL_COMPILE);
  254.   glShadeModel(GL_SMOOTH);  /* smooth minimizes seeing
  255.                                tessellation */
  256.   gluBeginPolygon(tobj);
  257.   for (i = 0; i < count; i++) {
  258.     vertex[0] = data[i][0];
  259.     vertex[1] = data[i][1];
  260.     vertex[2] = 0;
  261.     gluTessVertex(tobj, vertex, data[i]);
  262.   }
  263.   gluEndPolygon(tobj);
  264.   glEndList();
  265.   glNewList(edge, GL_COMPILE);
  266.   glShadeModel(GL_FLAT);  /* flat shade keeps angular hands
  267.                              from being "smoothed" */
  268.   glBegin(GL_QUAD_STRIP);
  269.   for (i = 0; i <= count; i++) {
  270.     /* mod function handles closing the edge */
  271.     glVertex3f(data[i % count][0], data[i % count][1], 0.0);
  272.     glVertex3f(data[i % count][0], data[i % count][1], thickness);
  273.     /* Calculate a unit normal by dividing by Euclidean
  274.        distance. We * could be lazy and use
  275.        glEnable(GL_NORMALIZE) so we could pass in * arbitrary
  276.        normals for a very slight performance hit. */
  277.     dx = data[(i + 1) % count][1] - data[i % count][1];
  278.     dy = data[i % count][0] - data[(i + 1) % count][0];
  279.     len = sqrt(dx * dx + dy * dy);
  280.     glNormal3f(dx / len, dy / len, 0.0);
  281.   }
  282.   glEnd();
  283.   glEndList();
  284.   glNewList(whole, GL_COMPILE);
  285.   glFrontFace(GL_CW);
  286.   glCallList(edge);
  287.   glNormal3f(0.0, 0.0, -1.0);  /* constant normal for side */
  288.   glCallList(side);
  289.   glPushMatrix();
  290.   glTranslatef(0.0, 0.0, thickness);
  291.   glFrontFace(GL_CCW);
  292.   glNormal3f(0.0, 0.0, 1.0);  /* opposite normal for other side */
  293.   glCallList(side);
  294.   glPopMatrix();
  295.   glEndList();
  296. }
  297.  
  298. /* Enumerants for refering to display lists. */
  299. typedef enum {
  300.   RESERVED, BODY_SIDE, BODY_EDGE, BODY_WHOLE, ARM_SIDE, ARM_EDGE, ARM_WHOLE,
  301.   LEG_SIDE, LEG_EDGE, LEG_WHOLE, EYE_SIDE, EYE_EDGE, EYE_WHOLE, surf_tri,
  302.                                                                 surf_strip
  303. } displayLists;
  304.  
  305. static GLfloat stripColor[] = { 0.8, 0.9, 0.9, 1.0};
  306.  
  307. static void
  308. makeDinosaur(void)
  309. {
  310.   extrudeSolidFromPolygon(body, sizeof(body), bodyWidth,
  311.     BODY_SIDE, BODY_EDGE, BODY_WHOLE);
  312.   extrudeSolidFromPolygon(arm, sizeof(arm), bodyWidth / 4,
  313.     ARM_SIDE, ARM_EDGE, ARM_WHOLE);
  314.   extrudeSolidFromPolygon(leg, sizeof(leg), bodyWidth / 2,
  315.     LEG_SIDE, LEG_EDGE, LEG_WHOLE);
  316.   extrudeSolidFromPolygon(eye, sizeof(eye), bodyWidth + 0.2,
  317.     EYE_SIDE, EYE_EDGE, EYE_WHOLE);
  318. }
  319.  
  320. static void
  321. drawDinosaur(void)
  322.  
  323. {
  324.   glPushMatrix();
  325.   /* Translate the dinosaur to be at (0,8,0). */
  326.   glTranslatef(-8, 0, -bodyWidth / 2);
  327.   glTranslatef(0.0, jump, 0.0);
  328.   if (lightSwitch)
  329.     glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
  330.   else
  331.     glColor4fv(skinColor);
  332.  
  333.   glCallList(BODY_WHOLE);
  334.   glTranslatef(0.0, 0.0, bodyWidth);
  335.   glCallList(ARM_WHOLE);
  336.   glCallList(LEG_WHOLE);
  337.   glTranslatef(0.0, 0.0, -bodyWidth - bodyWidth / 4);
  338.   glCallList(ARM_WHOLE);
  339.   glTranslatef(0.0, 0.0, -bodyWidth / 4);
  340.   glCallList(LEG_WHOLE);
  341.   glTranslatef(0.0, 0.0, bodyWidth / 2 - 0.1);
  342.   if (lightSwitch)
  343.     glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);
  344.   else
  345.     glColor4fv(eyeColor);
  346.  
  347.   glCallList(EYE_WHOLE);
  348.   glPopMatrix();
  349. }
  350.  
  351. #define MAXVERTS 10000
  352.  
  353. static GLfloat verts[MAXVERTS][3];
  354. static GLfloat norms[MAXVERTS][3];
  355. int numverts;
  356. GLboolean use_vertex_arrays = GL_FALSE;
  357.  
  358. static void drawTristrips()
  359. {
  360.    GLuint i;
  361.  
  362. #ifdef GL_EXT_vertex_array
  363.    if (use_vertex_arrays) {
  364.       glDrawArraysEXT( GL_TRIANGLE_STRIP, 0, numverts );
  365.    }
  366.    else {
  367. #endif
  368.       glBegin( GL_TRIANGLE_STRIP );
  369.       for (i=0;i<numverts;i++) {
  370.          glNormal3fv( norms[i] );
  371.          glVertex3fv( verts[i] );
  372.       }
  373.       glEnd();
  374. #ifdef GL_EXT_vertex_array
  375.    }
  376. #endif
  377.  }
  378.  
  379.  
  380.  
  381. static void drawTriangles()
  382. {
  383.   int i;
  384.   
  385.   glBegin(GL_TRIANGLES);
  386.  
  387.   for (i = 0; i < numverts - 2; ++i)
  388.     {
  389.       glNormal3fv(norms[i]);
  390.       glVertex3fv(verts[i]);
  391.  
  392.       glNormal3fv(norms[i + 1]);
  393.       glVertex3fv(verts[i + 1]);
  394.  
  395.       glNormal3fv(norms[i + 2]);
  396.       glVertex3fv(verts[i + 2]);
  397.     }
  398.   glEnd();
  399. }
  400.  
  401. static void initTriangles(char *filename)
  402. {
  403.   FILE *f;
  404.   
  405.   f = fopen(filename,"r");
  406.   if (!f) {
  407.     printf("couldn't read %s\n", filename);
  408.     exit(1);
  409.   }
  410.   
  411.   numverts = 0;
  412.   while (!feof(f) && numverts<MAXVERTS) {
  413.     fscanf( f, "%f %f %f  %f %f %f",
  414.        &verts[numverts][0], &verts[numverts][1], &verts[numverts][2],
  415.        &norms[numverts][0], &norms[numverts][1], &norms[numverts][2] );
  416.  
  417.     /* Scale the iso model so that it occupies some space.... */
  418.     verts[numverts][0] =  verts[numverts][0] * 10;
  419.     verts[numverts][1] =  verts[numverts][1] * 10 + 10;
  420.     verts[numverts][2] =  verts[numverts][2] * 10;
  421.  
  422.     numverts++;
  423.   }
  424.   numverts--;
  425.   
  426.   printf("%d vertices, %d triangles\n", numverts, numverts-2);
  427.   fclose(f);
  428.  
  429. }
  430.  
  431. static void drawObject()
  432. {
  433.   switch (object_type)
  434.     {
  435.     case 0:
  436.       drawDinosaur();
  437.       break;
  438.     case 1:
  439.       if (shade_type)
  440.     glShadeModel(GL_FLAT);
  441.       else
  442.     glShadeModel(GL_SMOOTH);
  443.  
  444.       glDisable(GL_CULL_FACE);
  445.       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, stripColor);
  446.       drawTriangles();
  447.       glEnable(GL_CULL_FACE);
  448.       break;
  449.     case 2:
  450.       if (shade_type)
  451.     glShadeModel(GL_FLAT);
  452.       else
  453.     glShadeModel(GL_SMOOTH);
  454.  
  455.       glDisable(GL_CULL_FACE);
  456.       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, stripColor);
  457.       drawTristrips();
  458.       glEnable(GL_CULL_FACE);
  459.       break;
  460.     case 3:
  461.       if (shade_type)
  462.     glShadeModel(GL_FLAT);
  463.       else
  464.     glShadeModel(GL_SMOOTH);
  465.  
  466.       glDisable(GL_CULL_FACE);
  467.       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, stripColor);
  468.       glCallList(surf_tri);
  469.       glEnable(GL_CULL_FACE);
  470.       break;
  471.     case 4:
  472.       if (shade_type)
  473.     glShadeModel(GL_FLAT);
  474.       else
  475.     glShadeModel(GL_SMOOTH);
  476.  
  477.       glDisable(GL_CULL_FACE);
  478.       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, stripColor);
  479.       glCallList(surf_strip);
  480.       glEnable(GL_CULL_FACE);
  481.       break;
  482.     }
  483. }
  484.  
  485. static GLfloat floorVertices[4][3] = {
  486.   { -20.0, 0.0, 20.0 },
  487.   { 20.0, 0.0, 20.0 },
  488.   { 20.0, 0.0, -20.0 },
  489.   { -20.0, 0.0, -20.0 },
  490. };
  491.  
  492. /* Draw a floor (possibly textured). */
  493. static void
  494. drawFloor(void)
  495. {
  496.   glDisable(GL_LIGHTING);
  497.  
  498.   if (useTexture) {
  499.     glEnable(GL_TEXTURE_2D);
  500.   }
  501.  
  502.   glBegin(GL_QUADS);
  503.     glTexCoord2f(0.0, 0.0);
  504.     glVertex3fv(floorVertices[0]);
  505.     glTexCoord2f(0.0, 16.0);
  506.     glVertex3fv(floorVertices[1]);
  507.     glTexCoord2f(16.0, 16.0);
  508.     glVertex3fv(floorVertices[2]);
  509.     glTexCoord2f(16.0, 0.0);
  510.     glVertex3fv(floorVertices[3]);
  511.   glEnd();
  512.  
  513.   if (useTexture) {
  514.     glDisable(GL_TEXTURE_2D);
  515.   }
  516.  
  517.   if (lightSwitch)
  518.     glEnable(GL_LIGHTING);
  519. }
  520.  
  521. static GLfloat floorPlane[4];
  522. static GLfloat floorShadow[4][4];
  523.  
  524. static void
  525. redraw(void)
  526. {
  527.   int start, end;
  528.  
  529.   if (reportSpeed)
  530.     {
  531.       start = glutGet(GLUT_ELAPSED_TIME);
  532.     }
  533.  
  534.   if (lightSwitch) 
  535.     {
  536.       glEnable(GL_LIGHT0);
  537.       glEnable(GL_LIGHTING);
  538.     } 
  539.   else
  540.     {
  541.       glDisable(GL_LIGHT0);
  542.       glDisable(GL_LIGHTING);
  543.     }
  544.   
  545.   /* Clear; default stencil clears to zero. */
  546.   if ((stencilReflection && renderReflection) ||
  547.       (stencilShadow && renderShadow))
  548.     {
  549.       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
  550.           GL_STENCIL_BUFFER_BIT);
  551.     }
  552.   else
  553.     {
  554.       /* Avoid clearing stencil when not using it. */
  555.       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  556.     }
  557.   
  558.   /* Reposition the light source. */
  559.   lightPosition[0] = 12*cos(lightAngle);
  560.   lightPosition[1] = lightHeight;
  561.   lightPosition[2] = 12*sin(lightAngle);
  562.   if (directionalLight)
  563.     {
  564.       lightPosition[3] = 0.0;
  565.     }
  566.   else
  567.     {
  568.       lightPosition[3] = 1.0;
  569.     }
  570.   
  571.   shadowMatrix(floorShadow, floorPlane, lightPosition);
  572.   
  573.   glPushMatrix();
  574.   /* Perform scene rotations based on user mouse input. */
  575.   glRotatef(angle2, 1.0, 0.0, 0.0);
  576.   glRotatef(angle, 0.0, 1.0, 0.0);
  577.   
  578.   /* Tell GL new light source position. */
  579.   glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  580.   
  581.   if (renderReflection)
  582.     {
  583.       if (stencilReflection)
  584.     {
  585.       /* We can eliminate the visual "artifact" of seeing the "flipped"
  586.          dinosaur underneath the floor by using stencil.  The idea is
  587.          draw the floor without color or depth update but so that 
  588.          a stencil value of one is where the floor will be.  Later when
  589.          rendering the dinosaur reflection, we will only update pixels
  590.          with a stencil value of 1 to make sure the reflection only
  591.          lives on the floor, not below the floor. */
  592.       
  593.       /* Don't update color or depth. */
  594.       glDisable(GL_DEPTH_TEST);
  595.       glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  596.       
  597.       /* Draw 1 into the stencil buffer. */
  598.       glEnable(GL_STENCIL_TEST);
  599.       glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
  600.       glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
  601.       
  602.       /* Now render floor; floor pixels just get their stencil set to 1. */
  603.       drawFloor();
  604.       
  605.       /* Re-enable update of color and depth. */ 
  606.       glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  607.       glEnable(GL_DEPTH_TEST);
  608.       
  609.       /* Now, only render where stencil is set to 1. */
  610.       glStencilFunc(GL_EQUAL, 1, 0xffffffff);  /* draw if ==1 */
  611.       glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  612.     }
  613.       
  614.       glPushMatrix();
  615.       
  616.       /* The critical reflection step: Reflect dinosaur through the floor
  617.      (the Y=0 plane) to make a relection. */
  618.       glScalef(1.0, -1.0, 1.0);
  619.       
  620.       /* Reflect the light position. */
  621.       glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  622.       
  623.       /* To avoid our normals getting reversed and hence botched lighting
  624.      on the reflection, turn on normalize.  */
  625.       glEnable(GL_NORMALIZE);
  626.       glCullFace(GL_FRONT);
  627.       
  628.       /* Draw the reflected dinosaur. */
  629.       drawObject();
  630.       
  631.       /* Disable noramlize again and re-enable back face culling. */
  632.       glDisable(GL_NORMALIZE);
  633.       glCullFace(GL_BACK);
  634.       
  635.       glPopMatrix();
  636.       
  637.       /* Switch back to the unreflected light position. */
  638.       glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  639.       
  640.       if (stencilReflection) {
  641.         glDisable(GL_STENCIL_TEST);
  642.       }
  643.     }
  644.   
  645.   /* Back face culling will get used to only draw either the top or the
  646.      bottom floor.  This let's us get a floor with two distinct
  647.      appearances.  The top floor surface is reflective and kind of red.
  648.      The bottom floor surface is not reflective and blue. */
  649.   
  650.   /* Draw "bottom" of floor in blue. */
  651.   glFrontFace(GL_CW);  /* Switch face orientation. */
  652.   glColor4f(0.1, 0.1, 0.7, 1.0);
  653.   drawFloor();
  654.   glFrontFace(GL_CCW);
  655.   
  656.   if (renderShadow) 
  657.     {
  658.       if (stencilShadow)
  659.     {
  660.       /* Draw the floor with stencil value 3.  This helps us only 
  661.          draw the shadow once per floor pixel (and only on the
  662.          floor pixels). */
  663.       glEnable(GL_STENCIL_TEST);
  664.       glStencilFunc(GL_ALWAYS, 3, 0xffffffff);
  665.       glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  666.     }
  667.     }
  668.   
  669.   /* Draw "top" of floor.  Use blending to blend in reflection. */
  670.   glEnable(GL_BLEND);
  671.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  672.   glColor4f(0.7, 0.0, 0.0, 0.3);
  673.   glColor4f(1.0, 1.0, 1.0, 0.3);
  674.   drawFloor();
  675.   glDisable(GL_BLEND);
  676.   
  677.   if (renderDinosaur) 
  678.     {
  679.       /* Draw "actual" dinosaur, not its reflection. */
  680.       drawObject();
  681.     }
  682.   
  683.   if (renderShadow) 
  684.     {
  685.       
  686.       /* Render the projected shadow. */
  687.       
  688.       if (stencilShadow) 
  689.     {
  690.       
  691.       /* Now, only render where stencil is set above 2 (ie, 3 where
  692.          the top floor is).  Update stencil with 2 where the shadow
  693.          gets drawn so we don't redraw (and accidently reblend) the
  694.          shadow). */
  695.       glStencilFunc(GL_LESS, 2, 0xffffffff);  /* draw if ==1 */
  696.       glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
  697.     }
  698.       
  699.       /* To eliminate depth buffer artifacts, we use polygon offset
  700.      to raise the depth of the projected shadow slightly so
  701.      that it does not depth buffer alias with the floor. */
  702.       if (offsetShadow) 
  703.     {
  704.       switch (polygonOffsetVersion) 
  705.         {
  706.         case EXTENSION:
  707. #ifdef GL_EXT_polygon_offset
  708.           glEnable(GL_POLYGON_OFFSET_EXT);
  709.           break;
  710. #endif
  711. #ifdef GL_VERSION_1_1
  712.         case ONE_DOT_ONE:
  713.           glEnable(GL_POLYGON_OFFSET_FILL);
  714.           break;
  715. #endif
  716.         case MISSING:
  717.           /* Oh well. */
  718.           break;
  719.         }
  720.     }
  721.       
  722.       /* Render 50% black shadow color on top of whatever the
  723.          floor appareance is. */
  724.       glEnable(GL_BLEND);
  725.       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  726.       glDisable(GL_LIGHTING);  /* Force the 50% black. */
  727.       glColor4f(0.0, 0.0, 0.0, 0.5);
  728.       
  729.       glPushMatrix();
  730.       /* Project the shadow. */
  731.       glMultMatrixf((GLfloat *) floorShadow);
  732.       drawObject();
  733.       glPopMatrix();
  734.       
  735.       glDisable(GL_BLEND);
  736.  
  737.  
  738.       if (lightSwitch)
  739.     glEnable(GL_LIGHTING);
  740.       
  741.       if (offsetShadow) 
  742.     {
  743.       switch (polygonOffsetVersion) 
  744.         {
  745. #ifdef GL_EXT_polygon_offset
  746.         case EXTENSION:
  747.           glDisable(GL_POLYGON_OFFSET_EXT);
  748.           break;
  749. #endif
  750. #ifdef GL_VERSION_1_1
  751.         case ONE_DOT_ONE:
  752.           glDisable(GL_POLYGON_OFFSET_FILL);
  753.           break;
  754. #endif
  755.         case MISSING:
  756.           /* Oh well. */
  757.           break;
  758.         }
  759.     }
  760.       if (stencilShadow)
  761.     {
  762.       glDisable(GL_STENCIL_TEST);
  763.     }
  764.     }
  765.  
  766.   
  767.   if (lightSwitch)
  768.     {
  769.       glPushMatrix();
  770.       glDisable(GL_LIGHTING);
  771.       glColor3f(1.0, 1.0, 0.0);
  772.  
  773.       if (directionalLight)
  774.     {      
  775.       /* Draw an arrowhead. */
  776.       glDisable(GL_CULL_FACE);
  777.       glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
  778.       glRotatef(lightAngle * -180.0 / M_PI, 0, 1, 0);
  779.       glRotatef(atan(lightHeight/12) * 180.0 / M_PI, 0, 0, 1);
  780.       glBegin(GL_TRIANGLE_FAN);
  781.       glVertex3f(0, 0, 0);
  782.       glVertex3f(2, 1, 1);
  783.       glVertex3f(2, -1, 1);
  784.       glVertex3f(2, -1, -1);
  785.       glVertex3f(2, 1, -1);
  786.       glVertex3f(2, 1, 1);
  787.       glEnd();
  788.       /* Draw a white line from light direction. */
  789.       glColor3f(1.0, 1.0, 1.0);
  790.       glBegin(GL_LINES);
  791.       glVertex3f(0, 0, 0);
  792.       glVertex3f(5, 0, 0);
  793.       glEnd();
  794.       glEnable(GL_CULL_FACE);
  795.     }
  796.       else
  797.     {
  798.       /* Draw a yellow ball at the light source. */
  799.       glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
  800.       glutSolidSphere(1.0, 5, 5);
  801.     }
  802.       glPopMatrix();  /* From the lighting markers */
  803.     }
  804.   
  805.   glPopMatrix();
  806.   
  807.   if (reportSpeed)
  808.     {
  809.       glFinish();
  810.       end = glutGet(GLUT_ELAPSED_TIME);
  811.       printf("Speed %.3g frames/sec (%d ms)\n", 1000.0/(end-start), end-start);
  812.     }
  813.   
  814.   glutSwapBuffers();
  815. }
  816.  
  817. /* ARGSUSED2 */
  818. static void
  819. mouse(int button, int state, int x, int y)
  820. {
  821.   if (button == GLUT_LEFT_BUTTON) {
  822.     if (state == GLUT_DOWN) {
  823.       moving = 1;
  824.       startx = x;
  825.       starty = y;
  826.     }
  827.     if (state == GLUT_UP) {
  828.       moving = 0;
  829.     }
  830.   }
  831.   if (button == GLUT_MIDDLE_BUTTON) {
  832.     if (state == GLUT_DOWN) {
  833.       lightMoving = 1;
  834.       lightStartX = x;
  835.       lightStartY = y;
  836.     }
  837.     if (state == GLUT_UP) {
  838.       lightMoving = 0;
  839.     }
  840.   }
  841. }
  842.  
  843. /* ARGSUSED1 */
  844. static void
  845. motion(int x, int y)
  846. {
  847.   if (moving) {
  848.     angle = angle + (x - startx);
  849.     angle2 = angle2 + (y - starty);
  850.     startx = x;
  851.     starty = y;
  852.     glutPostRedisplay();
  853.   }
  854.   if (lightMoving) {
  855.     lightAngle += (x - lightStartX)/40.0;
  856.     lightHeight += (lightStartY - y)/20.0;
  857.     lightStartX = x;
  858.     lightStartY = y;
  859.     glutPostRedisplay();
  860.   }
  861. }
  862.  
  863. /* Advance time varying state when idle callback registered. */
  864. static void
  865. idle(void)
  866. {
  867.   static float time = 0.0;
  868.  
  869.   time = glutGet(GLUT_ELAPSED_TIME) / 500.0;
  870.  
  871.   jump = 4.0 * fabs(sin(time)*0.5);
  872.   if (!lightMoving) {
  873.     lightAngle += 0.03;
  874.   }
  875.   glutPostRedisplay();
  876. }
  877.  
  878. enum {
  879.   M_NONE, M_MOTION, M_LIGHT, M_TEXTURE, M_SHADOWS, M_REFLECTION, M_DINOSAUR,
  880.   M_STENCIL_REFLECTION, M_STENCIL_SHADOW, M_OFFSET_SHADOW,
  881.   M_POSITIONAL, M_DIRECTIONAL, M_PERFORMANCE
  882. };
  883.  
  884. static void
  885. controlLights(int value)
  886. {
  887.   switch (value) {
  888.   case M_NONE:
  889.     return;
  890.   case M_MOTION:
  891.     animation = 1 - animation;
  892.     if (animation) {
  893.       glutIdleFunc(idle);
  894.     } else {
  895.       glutIdleFunc(NULL);
  896.     }
  897.     break;
  898.   case M_LIGHT:
  899.     lightSwitch = !lightSwitch;
  900.     break;
  901.   case M_TEXTURE:
  902.     useTexture = !useTexture;
  903.     break;
  904.   case M_SHADOWS:
  905.     renderShadow = 1 - renderShadow;
  906.     break;
  907.   case M_REFLECTION:
  908.     renderReflection = 1 - renderReflection;
  909.     break;
  910.   case M_DINOSAUR:
  911.     renderDinosaur = 1 - renderDinosaur;
  912.     break;
  913.   case M_STENCIL_REFLECTION:
  914.     stencilReflection = 1 - stencilReflection;
  915.     break;
  916.   case M_STENCIL_SHADOW:
  917.     stencilShadow = 1 - stencilShadow;
  918.     break;
  919.   case M_OFFSET_SHADOW:
  920.     offsetShadow = 1 - offsetShadow;
  921.     break;
  922.   case M_POSITIONAL:
  923.     directionalLight = 0;
  924.     break;
  925.   case M_DIRECTIONAL:
  926.     directionalLight = 1;
  927.     break;
  928.   case M_PERFORMANCE:
  929.     reportSpeed = 1 - reportSpeed;
  930.     break;
  931.   }
  932.   glutPostRedisplay();
  933. }
  934.  
  935. /* When not visible, stop animating.  Restart when visible again. */
  936. static void 
  937. visible(int vis)
  938. {
  939.   if (vis == GLUT_VISIBLE) {
  940.     if (animation)
  941.       glutIdleFunc(idle);
  942.   } else {
  943.     if (!animation)
  944.       glutIdleFunc(NULL);
  945.   }
  946. }
  947.  
  948. static void benchmark(void)
  949. {
  950.   int i;
  951.   int start, end;
  952.   int light_type;
  953.   int shadow_type;
  954.  
  955.   reportSpeed = 0;
  956.  
  957.   printf("oglbench 1.0\n");
  958.   printf("Benchmark of OpenGL rendering in a variety of modes\n");
  959.   printf("Mode options are separated by colons, in the following order.\n");
  960.   printf("  Texture  [0 for off, 1 for on]\n");
  961.   printf("  Lighting [0 for off, 1 for direction, flat, 2 for dir smooth\n");
  962.   printf("            3 for position flat, 4 for position smooth]\n");
  963.   printf("  Shadows  [0 for off, 1 for on, 2 offset, 3 stencil,\n");
  964.   printf("            4 stencil and offset]\n");
  965.   printf("  Reflect  [0 off, 1 on]\n");
  966.   printf("  Object   [0 dinosaur, 1 triangles, 2 tri-strip, \n");
  967.   printf("            3 DL triangles, 4 DL tri-strip]\n");
  968.   printf("VENDOR: %s\n", glGetString(GL_VENDOR));
  969.   printf("RENDERER: %s\n", glGetString(GL_RENDERER));
  970.   printf("VERSION: %s\n", glGetString(GL_VERSION));
  971.   printf("EXTENSIONS: %s\n", glGetString(GL_EXTENSIONS));
  972.  
  973.   for (useTexture = 0; useTexture <= 1; ++useTexture)
  974.     {
  975.       for (light_type = 0; light_type <= 4; ++light_type)
  976.     {
  977.       switch (light_type)
  978.         {
  979.         case 0:
  980.           lightSwitch = 0;
  981.           break;
  982.         case 1:
  983.           lightSwitch = 1;
  984.           directionalLight = 0;
  985.           shade_type = 0;
  986.           break;
  987.         case 2:
  988.           lightSwitch = 1;
  989.           directionalLight = 0;
  990.           shade_type = 1;
  991.           break;
  992.         case 3:
  993.           lightSwitch = 1;
  994.           directionalLight = 1;
  995.           shade_type = 0;
  996.           break;
  997.         case 4:
  998.           lightSwitch = 1;
  999.           directionalLight = 1;
  1000.           shade_type = 1;
  1001.           break;
  1002.         }
  1003.       for (shadow_type = 0; shadow_type <= 4; ++shadow_type)
  1004.         {
  1005.           switch (shadow_type)
  1006.         {
  1007.         case 0:
  1008.           renderShadow = 0;
  1009.           break;
  1010.         case 1:
  1011.           renderShadow = 1;
  1012.           stencilShadow = 0;
  1013.           offsetShadow = 0;
  1014.           break;
  1015.         case 2:
  1016.           renderShadow = 1;
  1017.           stencilShadow = 0;
  1018.           offsetShadow = 1;
  1019.         case 3:
  1020.           renderShadow = 1;
  1021.           stencilShadow = 1;
  1022.           offsetShadow = 0;
  1023.         case 4:
  1024.           renderShadow = 1;
  1025.           stencilShadow = 1;
  1026.           offsetShadow = 1;
  1027.           break;
  1028.         }
  1029.           for (renderReflection = 0; renderReflection <= 1;
  1030.            ++renderReflection)
  1031.         {
  1032.           for (object_type = 0; object_type <= 4; ++object_type)
  1033.             {
  1034.               printf("%d:%d:%d:%d:%d  ",
  1035.                  useTexture,
  1036.                  light_type,
  1037.                  shadow_type,
  1038.                  renderReflection,
  1039.                  object_type);
  1040.               
  1041.               start = glutGet(GLUT_ELAPSED_TIME);
  1042.               for (i = 0; i < bench_iter; ++i)
  1043.             redraw();
  1044.               end = glutGet(GLUT_ELAPSED_TIME);
  1045.               printf("Speed %8.3f frames/sec (%8.3f ms)\n",
  1046.                  (1000.0/(end-start))*(float) bench_iter,
  1047.                  (end-start)/(float) bench_iter);
  1048.             }
  1049.         }
  1050.         }
  1051.     }
  1052.     }
  1053. }
  1054.  
  1055.  
  1056. /* Press any key to redraw; good when motion stopped and
  1057.    performance reporting on. */
  1058. /* ARGSUSED */
  1059. static void
  1060. key(unsigned char c, int x, int y)
  1061. {
  1062.   if (c == 27) {
  1063.     exit(0);  /* IRIS GLism, Escape quits. */
  1064.   }
  1065.   if (c == 'o')
  1066.     {
  1067.       ++object_type;
  1068.       if (object_type == 5)
  1069.     object_type = 0;
  1070.  
  1071.       printf("Use object type %d\n", object_type);
  1072.     }
  1073.   if (c == 's')
  1074.     {
  1075.       shade_type = 1 - shade_type;
  1076.     }
  1077.   if (c == 't')
  1078.     {
  1079.       useTexture = 1 - useTexture;
  1080.     }
  1081.   if (c == 'l')
  1082.     {
  1083.       lightSwitch = 1 - lightSwitch;
  1084.     }
  1085.   if (c == 'd')
  1086.     {
  1087.       directionalLight = 1 - directionalLight;
  1088.     }
  1089.   if (c == 'b')
  1090.     {
  1091.       benchmark();
  1092.     }
  1093.   if (c == 'z')
  1094.     {
  1095.       angle = angle - 5;
  1096.     }
  1097.   if (c == 'x')
  1098.     {
  1099.       angle = angle + 5;
  1100.     }
  1101.   if (c == 'Z')
  1102.     {
  1103.       angle2 = angle2 - 5;
  1104.     }
  1105.   if (c == 'X')
  1106.     {
  1107.       angle2 = angle2 + 5;
  1108.     }
  1109.  
  1110.   glutPostRedisplay();
  1111. }
  1112.  
  1113. /* Press any key to redraw; good when motion stopped and
  1114.    performance reporting on. */
  1115. /* ARGSUSED */
  1116. static void
  1117. special(int k, int x, int y)
  1118. {
  1119.   glutPostRedisplay();
  1120. }
  1121.  
  1122. static int
  1123. supportsOneDotOne(void)
  1124. {
  1125.   const char *version;
  1126.   int major, minor;
  1127.  
  1128.   version = (char *) glGetString(GL_VERSION);
  1129.   if (sscanf(version, "%d.%d", &major, &minor) == 2)
  1130.     return major >= 1 && minor >= 1;
  1131.   return 0;            /* OpenGL version string malformed! */
  1132. }
  1133.  
  1134. int
  1135. main(int argc, char **argv)
  1136. {
  1137.   int i;
  1138.  
  1139.   glutInit(&argc, argv);
  1140.   initTriangles("isosurf.dat");
  1141.  
  1142.   for (i=1; i<argc; i++) 
  1143.     {
  1144.       if (!strcmp("-linear", argv[i]))
  1145.     {
  1146.       linearFiltering = 1;
  1147.     }
  1148.  
  1149.       if (!strcmp("-mipmap", argv[i])) 
  1150.     {
  1151.       useMipmaps = 1;
  1152.     } 
  1153.  
  1154.       if (!strcmp("-ext", argv[i])) 
  1155.     {
  1156.       forceExtension = 1;
  1157.     }
  1158.       
  1159.       if (!strcmp("-iter", argv[i]))
  1160.     {
  1161.       bench_iter = atoi(argv[i + 1]);
  1162.       ++i;
  1163.     }
  1164.     }
  1165.  
  1166.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | 
  1167.               GLUT_STENCIL | GLUT_MULTISAMPLE);
  1168.  
  1169. #if 0
  1170.   /* In GLUT 4.0, you'll be able to do this an be sure to
  1171.      get 2 bits of stencil if the machine has it for you. */
  1172.   glutInitDisplayString("samples stencil>=2 rgb double depth");
  1173. #endif
  1174.  
  1175.   glutCreateWindow("Shadowy Leapin' Lizards");
  1176.   glutReshapeWindow(640, 400);
  1177.  
  1178.   if (glutGet(GLUT_WINDOW_STENCIL_SIZE) <= 1) {
  1179.     printf("dinoshade: Sorry, I need at least 2 bits of stencil.\n");
  1180.     exit(1);
  1181.   }
  1182.  
  1183.   /* Register GLUT callbacks. */
  1184.   glutDisplayFunc(redraw);
  1185.   glutMouseFunc(mouse);
  1186.   glutMotionFunc(motion);
  1187.   glutVisibilityFunc(visible);
  1188.   glutKeyboardFunc(key);
  1189.   glutSpecialFunc(special);
  1190.  
  1191.   glutCreateMenu(controlLights);
  1192.  
  1193.   glutAddMenuEntry("Toggle motion", M_MOTION);
  1194.   glutAddMenuEntry("-----------------------", M_NONE);
  1195.   glutAddMenuEntry("Toggle light", M_LIGHT);
  1196.   glutAddMenuEntry("Toggle texture", M_TEXTURE);
  1197.   glutAddMenuEntry("Toggle shadows", M_SHADOWS);
  1198.   glutAddMenuEntry("Toggle reflection", M_REFLECTION);
  1199.   glutAddMenuEntry("Toggle dinosaur", M_DINOSAUR);
  1200.   glutAddMenuEntry("-----------------------", M_NONE);
  1201.   glutAddMenuEntry("Toggle reflection stenciling", M_STENCIL_REFLECTION);
  1202.   glutAddMenuEntry("Toggle shadow stenciling", M_STENCIL_SHADOW);
  1203.   glutAddMenuEntry("Toggle shadow offset", M_OFFSET_SHADOW);
  1204.   glutAddMenuEntry("----------------------", M_NONE);
  1205.   glutAddMenuEntry("Positional light", M_POSITIONAL);
  1206.   glutAddMenuEntry("Directional light", M_DIRECTIONAL);
  1207.   glutAddMenuEntry("-----------------------", M_NONE);
  1208.   glutAddMenuEntry("Toggle performance", M_PERFORMANCE);
  1209.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  1210.   makeDinosaur();
  1211.  
  1212. #ifdef GL_VERSION_1_1
  1213.   if (supportsOneDotOne() && !forceExtension) {
  1214.     polygonOffsetVersion = ONE_DOT_ONE;
  1215.     glPolygonOffset(-2.0, -1.0);
  1216.   } else
  1217. #endif
  1218.   {
  1219. #ifdef GL_EXT_polygon_offset
  1220.   /* check for the polygon offset extension */
  1221.   if (glutExtensionSupported("GL_EXT_polygon_offset")) {
  1222.     polygonOffsetVersion = EXTENSION;
  1223.     glPolygonOffsetEXT(-0.1, -0.002);
  1224.   } else 
  1225. #endif
  1226.     {
  1227.       polygonOffsetVersion = MISSING;
  1228.       printf("\ndinoshine: Missing polygon offset.\n");
  1229.       printf("           Expect shadow depth aliasing artifacts.\n\n");
  1230.     }
  1231.   }
  1232.  
  1233.   glEnable(GL_CULL_FACE);
  1234.   glEnable(GL_DEPTH_TEST);
  1235.   glDisable(GL_TEXTURE_2D);
  1236.   glLineWidth(3.0);
  1237.  
  1238.   glMatrixMode(GL_PROJECTION);
  1239.   gluPerspective( /* field of view in degree */ 40.0,
  1240.   /* aspect ratio */ 1.0,
  1241.     /* Z near */ 20.0, /* Z far */ 100.0);
  1242.   glMatrixMode(GL_MODELVIEW);
  1243.   gluLookAt(0.0, 8.0, 60.0,  /* eye is at (0,0,30) */
  1244.     0.0, 8.0, 0.0,      /* center is at (0,0,0) */
  1245.     0.0, 1.0, 0.);      /* up is in postivie Y direction */
  1246.  
  1247.   glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  1248.   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
  1249.   glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
  1250.   glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  1251.   glEnable(GL_LIGHT0);
  1252.   glEnable(GL_LIGHTING);
  1253.  
  1254.   makeFloorTexture();
  1255.  
  1256.   /* Setup floor plane for projected shadow calculations. */
  1257.   findPlane(floorPlane, floorVertices[1], floorVertices[2], floorVertices[3]);
  1258.  
  1259.  
  1260.  
  1261.   /* Now make the call lists for either strips or triangles */
  1262.   glNewList(surf_tri, GL_COMPILE);
  1263.   drawTriangles();
  1264.   glEndList();
  1265.  
  1266.   glNewList(surf_strip, GL_COMPILE);
  1267.   drawTristrips();
  1268.   glEndList();
  1269.  
  1270.  
  1271.   glutMainLoop();
  1272.  
  1273.  
  1274.  
  1275.  
  1276.   return 0;             /* ANSI C requires main to return int. */
  1277. }
  1278.